package com.charger.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.domain.ChargerEnergyRes;
import com.charger.domain.QCharger;
import com.charger.domain.QChargerStation;
import com.charger.mapper.QChargerMapper;
import com.charger.service.QChargerStationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.domain.ChargerEnergy;
import com.charger.service.IChargerEnergyService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 能耗分析Controller
 * 
 * @author qkp
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/statistic.energy/energy")
public class ChargerEnergyController extends BaseController
{
    @Autowired
    private IChargerEnergyService chargerEnergyService;

    @Autowired
    private QChargerStationService chargerStationService;

    @Autowired
    private QChargerMapper qChargerMapper;

    /**
     * 查询能耗分析列表
     */
    @PreAuthorize("@ss.hasPermi('statistic.energy:energy:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChargerEnergy chargerEnergy)
    {
        startPage();
        List<ChargerEnergy> list = chargerEnergyService.selectChargerEnergyList(chargerEnergy);
        return getDataTable(list);
    }

    /**
     * 导出能耗分析列表
     */
    @PreAuthorize("@ss.hasPermi('statistic.energy:energy:export')")
    @Log(title = "能耗分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChargerEnergy chargerEnergy)
    {
        List<ChargerEnergy> list = chargerEnergyService.selectChargerEnergyList(chargerEnergy);
        ExcelUtil<ChargerEnergy> util = new ExcelUtil<ChargerEnergy>(ChargerEnergy.class);
        util.exportExcel(response, list, "能耗分析数据");
    }

    /**
     * 获取能耗分析详细信息
     */
    @PreAuthorize("@ss.hasPermi('statistic.energy:energy:query')")
    @GetMapping(value = "/{energyId}")
    public AjaxResult getInfo(@PathVariable("energyId") Long energyId)
    {
        return success(chargerEnergyService.selectChargerEnergyByEnergyId(energyId));
    }

    /**
     * 新增能耗分析
     */
    @PreAuthorize("@ss.hasPermi('statistic.energy:energy:add')")
    @Log(title = "能耗分析", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChargerEnergy chargerEnergy)
    {
        return toAjax(chargerEnergyService.insertChargerEnergy(chargerEnergy));
    }

    /**
     * 修改能耗分析
     */
    @PreAuthorize("@ss.hasPermi('statistic.energy:energy:edit')")
    @Log(title = "能耗分析", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChargerEnergy chargerEnergy)
    {
        return toAjax(chargerEnergyService.updateChargerEnergy(chargerEnergy));
    }

    /**
     * 删除能耗分析
     */
    @PreAuthorize("@ss.hasPermi('statistic.energy:energy:remove')")
    @Log(title = "能耗分析", businessType = BusinessType.DELETE)
	@DeleteMapping("/{energyIds}")
    public AjaxResult remove(@PathVariable Long[] energyIds)
    {
        return toAjax(chargerEnergyService.deleteChargerEnergyByEnergyIds(energyIds));
    }

    @GetMapping("/getl")
    @PreAuthorize("@ss.hasPermi('statistic.energy:energy:list')")
    public Object getList(@RequestParam Map map) throws ParseException {
        Map<String, Object> res = chargerEnergyService.getList(map);
        return AjaxResult.success(res);
    }

    @GetMapping("/station")
    public Object getChargerStation(){
        List<QChargerStation> chargerStations = chargerStationService.getChargerStations();
        return AjaxResult.success(chargerStations);
    }

    @GetMapping("/chargers")
    public Object getChargers(
            @RequestParam(value = "stationId", required = false) Integer stationId){
        QueryWrapper<QCharger> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chargingStation_id", stationId);
        List<QCharger> qChargers = qChargerMapper.selectList(queryWrapper);
        return AjaxResult.success(qChargers);
    }
}
